package com.sys.web.UI;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sys.dao.MsgDao;
import com.sys.dao.impl.MsgDaoImpl;
import com.sys.domain.File;
import com.sys.domain.Msg;
import com.sys.domain.PageBean;
import com.sys.service.FileService;
import com.sys.service.impl.FileServiceImpl;

@WebServlet("/homeUI")
public class HomeUI extends HttpServlet {
	//未每个类别的页面设置一个页对象
	private PageBean learn = new PageBean();
	private PageBean play = new PageBean();
	private PageBean ide = new PageBean();
	private PageBean office = new PageBean();
	FileService fSI = new FileServiceImpl();
	private static final long serialVersionUID = 1L;
	private Integer onePageNum = 9;
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String line = null;
		List<Msg> allMsg = null;
		//设置每页条数
		learn.setOnePageNum(onePageNum);
		play.setOnePageNum(onePageNum);
		ide.setOnePageNum(onePageNum);
		office.setOnePageNum(onePageNum);
		
		//处理各类型的页码
		String method = request.getParameter("method");
		String fileType = request.getParameter("fileType");
		if(fileType!=null&&fileType.equals("learn")) {
			if(method!=null&&method.equals("next")) {
				learn.next();
			}else if (method!=null&&method.equals("prior")) {
				learn.prior();
			}
		}else if(fileType!=null&&fileType.equals("play")) {
			if(method!=null&&method.equals("next")) {
				play.next();
			}else if (method!=null&&method.equals("prior")) {
				play.prior();
			}
		}else if(fileType!=null&&fileType.equals("ide")) {
			if(method!=null&&method.equals("next")) {
				ide.next();
			}else if (method!=null&&method.equals("prior")) {
				ide.prior();
			}
		}else if(fileType!=null&&fileType.equals("office")) {
			if(method!=null&&method.equals("next")) {
				office.next();
			}else if (method!=null&&method.equals("prior")) {
				office.prior();
			}
		}else {
			learn.first();
			play.first();
			ide.first();
			office.first();
			fileType = "learn";
			//得到公告中的信息
			java.io.File notice = new java.io.File("E:\\Java\\Workspace_Project\\Sharing-Your-Story\\src\\notice");
			InputStreamReader reader = new InputStreamReader(new FileInputStream(notice));
			BufferedReader br = new BufferedReader(reader);
			line = br.readLine();
			br.close();
			MsgDao msgDao = new MsgDaoImpl();
			try {
				allMsg = msgDao.getAllMsg();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			
		}
		
		//设置每个类别的页数
		List<File> learnInfo = new ArrayList<>();
		List<File> playInfo = new ArrayList<>();
		List<File> ideInfo = new ArrayList<>();
		List<File> officeInfo = new ArrayList<>();
		try {
			new FileServiceImpl().sortFiles(learnInfo, playInfo, ideInfo, officeInfo);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		learn.setAllPage(learnInfo.size()%onePageNum==0?learnInfo.size()/onePageNum:learnInfo.size()/onePageNum + 1);
		play.setAllPage(playInfo.size()%onePageNum==0?playInfo.size()/onePageNum:playInfo.size()/onePageNum + 1);
		ide.setAllPage(ideInfo.size()%onePageNum==0?ideInfo.size()/onePageNum:ideInfo.size()/onePageNum + 1);
		office.setAllPage(officeInfo.size()%onePageNum==0?officeInfo.size()/onePageNum:officeInfo.size()/onePageNum + 1);
		
		//第一次设置全部的信息到PageBean中
		learn.setFilesInfo(learnInfo);
		play.setFilesInfo(playInfo);
		ide.setFilesInfo(ideInfo);
		office.setFilesInfo(officeInfo);
		
		//获得各类别的当前页面的信息
		learnInfo = fSI.getInfoFromBeginToEnd(learnInfo, learn.getBegin(), learn.getEnd());
		playInfo = fSI.getInfoFromBeginToEnd(playInfo, play.getBegin(), play.getEnd());
		ideInfo = fSI.getInfoFromBeginToEnd(ideInfo, ide.getBegin(), ide.getEnd());
		officeInfo = fSI.getInfoFromBeginToEnd(officeInfo, office.getBegin(), office.getEnd());
		
		learn.setFilesInfo(learnInfo);
		play.setFilesInfo(playInfo);
		ide.setFilesInfo(ideInfo);
		office.setFilesInfo(officeInfo);
		
		//设置当前页码
		request.setAttribute("msgs", allMsg);
		request.setAttribute("notice", line);
		//存储四个类型的文件
		request.setAttribute("fileType", upperCase(fileType));
		request.setAttribute("learn", learn);
		request.setAttribute("play", play);
		request.setAttribute("ide", ide);
		request.setAttribute("office", office);
		request.getRequestDispatcher("/WEB-INF/zuserpages/home.jsp").forward(request, response);
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}
	
	/**
	 * @Title: upperCase 
	 * @Description: TODO(首字母大写) 
	 * @param str
	 * @return String
	 */
	private static String upperCase(String str) {  
	    char[] ch = str.toCharArray();  
	    if (ch[0] >= 'a' && ch[0] <= 'z') {  
	        ch[0] = (char) (ch[0] - 32);  
	    }  
	    return new String(ch);  
	}  
}